package com.xxkk.net.TcpNetty;

import com.ebo.synframework.nettybase.dispatcher.CmdDispatcher;
import com.ebo.synframework.nettybase.dispatcher.ReqCmd;
import com.ebo.synframework.synroom.executor.pool.SynRoomThreadPool;
import com.xxkk.gameCore.global.globalServer.ServerContext;
import com.xxkk.net.websocketNetty.Player;
import com.xxkk.utils.BusinessPool;
import org.apache.log4j.Logger;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.*;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author xxkk
 * @version 1.0.0
 * @title TcpMsgHandler
 * @description ʕ•ᴥ•ʔ
 * @create 2024/5/22 17:44
 **/
@Component
public abstract class TcpMsgHandler extends SimpleChannelUpstreamHandler implements ApplicationContextAware {
    private final static Logger logger = Logger.getLogger(TcpMsgHandler.class);
    protected SynRoomThreadPool synRoomThreadPool;
    @Autowired
    protected ServerContext serverContext;
    @Autowired
    private BusinessPool businessPool;// 定时任务使用
    private Executor executor = Executors.newCachedThreadPool();// 用来执行一些不需要限制请求频率的任务
    @Autowired
    private CmdDispatcher cmdDispatcher;
    private ApplicationContext ac;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ac = applicationContext;
    }
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e){
        try {
            // 默认服务器完全启动可接收消息
            Player player = (Player) ctx.getAttachment();
            player.setLastReqTime(System.currentTimeMillis());// 刷新响应时间
            if (e.getMessage() instanceof ReqCmd) {

            } else {
                logger.info("收到的消息不是ReqCmd帧信息#" + e.getMessage());
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
        logger.error("netty meet an uncatch error ", e.getCause());
    }
    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {

    }
    @Override
    public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) {

    }

    public abstract void playerDisconnect(Player player);


}
